package com.example.jiaju;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager2.widget.ViewPager2;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


import com.example.jiaju.adapter.MainFragmentStateAdapter;
import com.example.jiaju.databinding.ActivityMainBinding;
import com.example.jiaju.ui.BuildingFragment;
import com.example.jiaju.ui.ConsultFragment;
import com.example.jiaju.ui.ForumFragment;
import com.example.jiaju.ui.FurnitureFragment;
import com.example.jiaju.ui.HomeFragment;
import com.example.jiaju.ui.HomeViewFragment;
import com.example.jiaju.ui.MessageFragment;
import com.example.jiaju.ui.MyFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity {

    private BottomNavigationView mButtomNavView;
    private ActivityMainBinding mBinding;
    private ViewPager2 mViewPager2;

    private List<Fragment> mFragmentList;
    private MainFragmentStateAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);

        initView();

        initListener();

    }

    private void initListener() {
        mButtomNavView.setOnItemSelectedListener(item -> {
            switch (item.getItemId()){
                case R.id.homeFragment:mViewPager2.setCurrentItem(0);break;
                case R.id.forumFragment:mViewPager2.setCurrentItem(1);break;
                case R.id.messageFragment:mViewPager2.setCurrentItem(2);break;
                case R.id.myFragment:mViewPager2.setCurrentItem(3);break;
                default:break;
            }
            return true;
        });
        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:mButtomNavView.setSelectedItemId(R.id.homeFragment);break;
                    case 1:mButtomNavView.setSelectedItemId(R.id.forumFragment);break;
                    case 2:mButtomNavView.setSelectedItemId(R.id.messageFragment);break;
                    case 3:mButtomNavView.setSelectedItemId(R.id.myFragment);break;
                    default:break;
                }
            }
        });
    }

    private void initView() {
        mViewPager2 = mBinding.mainViewpager2;
        mButtomNavView=mBinding.navMain;

        mFragmentList=new ArrayList<>();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new ForumFragment());
        mFragmentList.add(new ConsultFragment());
        mFragmentList.add(new MyFragment());
        mAdapter=new MainFragmentStateAdapter(this,mFragmentList);
        mViewPager2.setAdapter(mAdapter);

        //沉静式状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }
}